package com.ydgk.controller;

import com.ydgk.bean.View;
import com.ydgk.service.ViewService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/view")
@Controller
public class ViewController {
    @Autowired
    private ViewService viewService;

    private static final String UPLOAD_DIRECTORY = "upload/view";
    public static Map<String,MultipartFile> fileForView = new HashMap<String, MultipartFile>();

    @ModelAttribute
    public void getView(@RequestParam(value = "vid" , required = false , defaultValue = "0") Integer vid, Map map){
        if(vid != 0){
            View view = viewService.findById(vid);
            map.put("view",view);
        }
    }

    @ResponseBody
    @RequestMapping("/viewUpload")
    public AjaxResult viewUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForView.put(file.getOriginalFilename(),file);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/fileDelete")
    public AjaxResult fileDelete(String name) throws IOException {
        this.fileForView.remove(name);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteView/{vid}", method = RequestMethod.DELETE)
    public AjaxResult deleteView(@PathVariable("vid")Integer vid, Integer fid, View view, HttpServletRequest request){
        ServletContext application = request.getServletContext();
        AjaxResult ajaxResult = new AjaxResult();
        int i = viewService.deleteByIdAndFid(vid,fid);
        if(i > 0){
            String vphoto = view.getVphoto();
            new File(application.getRealPath("/"+vphoto)).delete();
            ajaxResult.setSuccess(true);
        }else{
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/insertView")
    public AjaxResult insertView(HttpServletRequest request, Integer fid) throws IOException {
        Collection<MultipartFile> views = fileForView.values();
        View view = new View();
        String viewname = "";
        for (MultipartFile file : views) {
            viewname = Tool.upload(request,file,UPLOAD_DIRECTORY);
            view.setVphoto(UPLOAD_DIRECTORY + "/" + viewname);
            view.setF_fid(fid);
            viewService.insert(view);
        }
        clear();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    public static void clear(){
        fileForView.clear();
    }
}
